package org.vellos.halide;

import org.vellos.halide.interpret.Interpreter;
import org.vellos.halide.vm.VirtualMachine;

public class REPLFrame extends javax.swing.JFrame {

  VirtualMachine vm;
  Interpreter interpreter;

  public REPLFrame() {
    initComponents();

    this.setLocation(400, 200);

    vm = new VirtualMachine();
    interpreter = new Interpreter(vm);
    outputTextPane.setText("Ready.");
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    outputTextPane = new javax.swing.JTextPane();
    inputTextField = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Halide Interpreter");

    outputTextPane.setEditable(false);
    outputTextPane.setAutoscrolls(true);
    jScrollPane1.setViewportView(outputTextPane);

    inputTextField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
    inputTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
    inputTextField.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyTyped(java.awt.event.KeyEvent evt) {
        inputTextFieldKeyTyped(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
          .addComponent(inputTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void inputTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputTextFieldKeyTyped
    if (evt.getKeyChar() == '\n') {
      String expression = inputTextField.getText();
      outputTextPane.setText(outputTextPane.getText() + "\n" + "> " + expression);
      inputTextField.setText("");
      try {
        outputTextPane.setText(outputTextPane.getText() + "\n" + interpreter.evaluate(expression));
      } catch (Exception ex) {
        outputTextPane.setText(outputTextPane.getText() + "\n" + ex.getClass().getName() + ": " + ex.getMessage());
      }
    }
  }//GEN-LAST:event_inputTextFieldKeyTyped

  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

      @Override
      public void run() {
        new REPLFrame().setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTextField inputTextField;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextPane outputTextPane;
  // End of variables declaration//GEN-END:variables
}
